package com.zhr.tea.view.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONObject;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zhr.tea.R;
import com.zhr.tea.entity.shouye.Chaquan;
import com.zhr.tea.view.adapter.ShouyeChaquanRemenAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

/**
 * A simple {@link Fragment} subclass.
 */
public class RemenFragment extends Fragment {

    @BindView(R.id.xlv)
    XRecyclerView xlv;
    private List<Chaquan.ShareListBean> data = new ArrayList<>();
    private ShouyeChaquanRemenAdapter adapter;

    public RemenFragment() {
        // Required empty public constructor
    }

    public static RemenFragment newInstance() {

        Bundle args = new Bundle();

        RemenFragment fragment = new RemenFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_remen, container, false);
        ButterKnife.bind(this, view);
        initView();
        initData();
        refresh();
        return view;
    }

    private void refresh() {
        xlv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initData();
            }

            @Override
            public void onLoadMore() {

            }
        });
    }

    private void initData() {
        RequestParams params = new RequestParams();
        params.addHeader("Content-Type", "application/json");
        JSONObject obj = new JSONObject();
        obj.put("maxSize", "10");
        obj.put("beginId", "");
        obj.put("direction", "0");
        obj.put("memberId", "mt20161031215643782");
        obj.put("type", "1");
        params.applicationJson(obj);
        HttpRequest.post("http://www.teapaopao.com:8099/tea/clientShare/searchShareInfoList.do", params, new BaseHttpRequestCallback<Chaquan>() {
            @Override
            protected void onSuccess(Chaquan chaquan) {
                super.onSuccess(chaquan);
                data.addAll(chaquan.getShareList());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                super.onFailure(errorCode, msg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
            }
        });
        xlv.refreshComplete();
        xlv.loadMoreComplete();
    }

    private void initView() {
        adapter = new ShouyeChaquanRemenAdapter(getContext(), data);
        xlv.setLayoutManager(new LinearLayoutManager(getContext()));
        xlv.setAdapter(adapter);
    }

}
